package net.fpeg.kotlin.msa.wordbase.service

import net.fpeg.kotlin.msa.wordbase.dao.WordBaseDao
import net.fpeg.kotlin.msa.wordbase.entity.*
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import javax.transaction.Transactional

@Service
class WordBaseService {
    @Autowired
    lateinit var wordBaseDao: WordBaseDao

    @Autowired
    lateinit var wordService: WordService

    @Autowired
    lateinit var wordTagService: WordTagService

    @Autowired
    lateinit var wordUserService: WordUserService

    @Autowired
    lateinit var wordNoteService: WordNoteService

    fun save(wordBases: List<WordBase>): MutableList<WordBase> = wordBaseDao.saveAll(wordBases)
    fun remove(wordBases: List<WordBase>) = wordBaseDao.deleteAll(wordBases)


    fun get(id: Long, wordValue: String, tagValue: String) =
            wordBaseDao.getByWordUser_IdAndWord_ValueAndWordTag_Value(id, wordValue, tagValue)

    fun get(id: Long, word: Word, tag: WordTag) =
            wordBaseDao.getByWordUser_IdAndWordAndWordTag(id, word, tag)

    fun getOne(id: Long, word: Word, tag: WordTag) =
            get(id, word, tag) ?: throw Exception("找不到也")

    fun getOne(id: Long, wordValue: String, tagValue: String) =
            get(id, wordValue, tagValue) ?: throw Exception("找不到也")

    fun find(uid: Long, wordValue: String) =
            wordBaseDao.findByWordUser_IdAndWord_Value(uid, wordValue)

    fun findTag(uid: Long, tagValue: String) =
            wordBaseDao.findByWordUser_IdAndWordTag_Value(uid, tagValue)


    fun add(id: Long, word: Word, tag: WordTag) = get(id, word, tag) ?: wordBaseDao.save(
            WordBase(null, wordUserService.getOne(id), word, tag)
    )

    private fun add(id: Long, wordValue: String, tagValue: String) = add(id, wordService.add(wordValue), wordTagService.add(id, tagValue))

    fun add(uid: Long, wordValue: String, tagValue: String, checkDefaultTag: Boolean) = when (checkDefaultTag) {
        true -> {
            if (tagValue != "默认") add(uid, wordValue, "默认")
            add(uid, wordValue, tagValue)
        }
        false -> add(uid, wordValue, tagValue)
    }


//    fun add(word: Word, wordUser: WordUser, wordTag: WordTag) {
//
//    }

    fun edit(uid: Long, wordValue: String, oldTagValue: String, newTagValue: String) =
            getOne(uid, wordValue, oldTagValue).apply { wordTag.value = newTagValue }.let { wordBaseDao.save(it) }


    fun getInfo(uid: Long, wordValue: String) = WordInfoBo(
            wordNoteService.getOne(uid, wordValue),
            wordTagService.find(uid, wordValue)
    )

    @Transactional
    fun remove(uid: Long, wordValue: String) =
            wordBaseDao.deleteByWordUser_IdAndWord_Value(uid, wordValue)


    @Transactional
    fun remove(uid: Long, wordValue: String, tagValue: String) =
            wordBaseDao.deleteByWordUser_IdAndWord_ValueAndWordTag_Value(uid, wordValue, tagValue)
}

data class WordInfoBo(val wordNote: WordNote, val wordTags: List<WordTag>)